package com.ks.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

/**
 * @author wr
 * @version V1.0
 * @date 2016-11-18 下午2:56
 * @desc 数据库增删改 从mysql数据库换成oracle时，94行注释放�?119 126
 */
@Component
public class DatabaseUtil {

    /**
     * 查询
     *
     * @param sql
     * @param param
     * @return
     */
    public List query(String sql, Object[] param) {

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List list = null;
        try {
            conn = JdbcUtil.getConnection();
            ps = conn.prepareStatement(sql);
            list = new ArrayList();
            if (param != null && param.length != 0) {
                for (int a = 0; a < param.length; a++) {
                    Object obj = param[a];
                    ps.setObject(a + 1, obj);
                }
            }
            rs = ps.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();
            while (rs.next()) {
                int columnNum = rsmd.getColumnCount();
                Map map = new HashMap();
                for (int i = 0; i < columnNum; i++) {
                    String realname = rsmd.getColumnName(i + 1);
                    map.put(realname, rs.getString(realname));
                }
                list.add(map);
            }
            return list;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            JdbcUtil.close(null, ps, rs, conn);
        }

    }

    /**
     * 更新或�?添加
     *
     * @param sql
     * @param param
     * @return
     */
    public int update(String sql, Object[] param) {
        int b = 1;
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtil.getConnection();
            ps = conn.prepareStatement(sql);
            if (param != null && param.length != 0) {
                for (int a = 0; a < param.length; a++) {
                    Object obj = param[a];
                    ps.setObject(a + 1, obj);
                }
            }
            ps.executeUpdate();
            // conn.commit();
        } catch (Exception e) {
            b = 0;
            e.printStackTrace();
        } finally {
            JdbcUtil.close(null, ps, rs, conn);
            return b;
        }

    }

    /**
     * 保存图片
     *
     * @param sql
     * @param zydm
     * @param bxdm
     * @param inputStream
     */
    public void saveBlob(String sql, String zydm, String bxdm, InputStream inputStream, String path) {
        PreparedStatement preparedStatement = null;
        Connection connection = null;

        try {
            connection = JdbcUtil.getConnection();
            // connection.setAutoCommit(false);
            preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setString(1, zydm);
            preparedStatement.setString(2, bxdm);
            preparedStatement.setBlob(3, inputStream);
            preparedStatement.setString(4, path);
            preparedStatement.executeUpdate();
            // connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null)
                    inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (preparedStatement != null)
                        preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (connection != null) {
                            connection.close();
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    /**
     * 保存图片(房屋管理)
     *
     * @param sql
     * @param fcdm
     * @param fcmc
     * @param type
     * @param floor
     * @param room
     * @param inputStream
     * @param path
     * @param preName
     */
    public void saveBlobHouse(String sql, String fcdm, String fcmc, String type, String floor, String room,
                              InputStream inputStream, String path, String preName) {
        PreparedStatement preparedStatement = null;
        Connection connection = null;

        try {
            connection = JdbcUtil.getConnection();
            // connection.setAutoCommit(false);
            preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setString(1, fcdm);
            preparedStatement.setString(2, fcmc);
            preparedStatement.setString(3, type);
            preparedStatement.setString(4, floor);
            preparedStatement.setString(5, room);
            preparedStatement.setBlob(6, inputStream);
            preparedStatement.setString(7, path);
            preparedStatement.setString(8, preName);
            preparedStatement.executeUpdate();
            // connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null)
                    inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (preparedStatement != null)
                        preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (connection != null) {
                            connection.close();
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    /**
     * 编辑
     *
     * @param sql
     * @param fcdm
     * @param fcmc
     * @param type
     * @param inputStream
     * @param path
     */
    public void editBlobHouse(String sql, String fcdm, String fcmc, String type, InputStream inputStream, String path) {
        PreparedStatement preparedStatement = null;
        Connection connection = null;

        try {
            connection = JdbcUtil.getConnection();
            // connection.setAutoCommit(false);
            preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setString(1, fcmc);
            preparedStatement.setString(2, type);
            preparedStatement.setBlob(3, inputStream);
            preparedStatement.setString(4, path);
            preparedStatement.setString(5, fcdm);
            preparedStatement.executeUpdate();
            // connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null)
                    inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (preparedStatement != null)
                        preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (connection != null) {
                            connection.close();
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    /**
     * 编辑(没有图片)
     *
     * @param sql
     * @param fcdm
     * @param fcmc
     * @param type
     */
    public void editBlobHouseWithOutPic(String sql, String fcdm, String fcmc, String type) {
        PreparedStatement preparedStatement = null;
        Connection connection = null;

        try {
            connection = JdbcUtil.getConnection();
            // connection.setAutoCommit(false);
            preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setString(1, fcmc);
            preparedStatement.setString(2, type);
            preparedStatement.setString(3, fcdm);
            preparedStatement.executeUpdate();
            // connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (preparedStatement != null)
                    preparedStatement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (connection != null) {
                        connection.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 查询
     *
     * @param sql
     * @param param
     * @return
     */
    public static byte[] queryBlob(String sql, Object[] param) {

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        byte[] bytes = null;
        Blob blob = null;
        try {
            conn = JdbcUtil.getConnection();
            ps = conn.prepareStatement(sql);
            if (param != null && param.length != 0) {
                for (int a = 0; a < param.length; a++) {
                    Object obj = param[a];
                    ps.setObject(a + 1, obj);
                }
            }
            rs = ps.executeQuery();
            while (rs.next()) {
                blob = rs.getBlob("DATA");
            }

            if (blob != null) {
                long size = blob.length();
                bytes = blob.getBytes(1, (int) size);
            }
            return bytes;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        } finally {
            JdbcUtil.close(null, ps, rs, conn);
        }

    }

    /**
     * 测试�?	 *
     *
     * @param sql
     * @param zydm
     * @param inputStream
     * @param path
     */
    public void upload(String sql, String zydm, String path, InputStream inputStream) {
        PreparedStatement preparedStatement = null;
        Connection connection = null;

        try {
            connection = JdbcUtil.getConnection();
            // connection.setAutoCommit(false);
            preparedStatement = connection.prepareStatement(sql);

            preparedStatement.setString(1, zydm);
            preparedStatement.setString(2, path);
            preparedStatement.setBlob(3, inputStream);
            preparedStatement.executeUpdate();
            // connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null)
                    inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (preparedStatement != null)
                        preparedStatement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (connection != null) {
                            connection.close();
                        }
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
